package net.fpeg.kotlin.msa.wordbase.controller

import net.fpeg.kotlin.msa.wordbase.dto.WordConditionDto
import net.fpeg.kotlin.msa.wordbase.dto.WordListDto
import net.fpeg.kotlin.msa.wordbase.dto.WordListIndexDto
import net.fpeg.kotlin.msa.wordbase.service.WordListService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/wordList")
class WordListController {

    @Autowired
    lateinit var wordListService: WordListService

    /**
     * 所有单词
     */
    @GetMapping
    fun fetch(
            @RequestHeader("USER_ID") id: Long,
            pageable: Pageable
    ) = wordListService.find(id, pageable).let { page ->
        WordListDto(page.totalElements.toInt(), page.toList().map { WordListIndexDto(it.value) })
    }

    /**
     * 有条件
     */
    @PostMapping("/condition")
    fun fetch(
            @RequestHeader("USER_ID") id: Long,
            @RequestBody wordConditionDto: WordConditionDto,
            pageable: Pageable
    ) = wordListService.find(id, wordConditionDto, pageable);


}